package com.bdqn.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * XML解析器
 *
 * @author LILIBO
 * @since 2024/6/28
 */
public class Test02XmlParser {

    public static void main(String[] args) {
        // 创建XML文档解析器工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            // 获得XML文档解析器
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 解析XML文件
            Document document = builder.parse("src/main/java/手机新闻.xml");
            // 通过标签名获取节点集合
            NodeList nodeList = document.getElementsByTagName("pubDate");
            Element pubDateElement = (Element) nodeList.item(0);
            String textContent = pubDateElement.getTextContent();
            System.out.println("获取到发布日期：" + textContent.trim());
        } catch (ParserConfigurationException | IOException | SAXException e) {
            throw new RuntimeException(e);
        }
    }

}
